package com.dangdi.show.dao;

import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

public interface WechatUserDao {
    //查询openid
    @Select("select * from wechat_user where open_id=#{open_id} ")
    Map<String, Object> FindOpenIdByOpenId(@Param("open_id") String open_id);

    //插入openid
    @Insert("insert into wechat_user values(#{open_id})")
    void InserOpenId(@Param("open_id") String open_id);

    //添加地址
    @Insert("insert into address values(null,#{user_name},#{user_tel},#{user_sheng},#{user_shi},#{user_qu},#{user_add},#{open_id},0)")
    void AddWecharUserAddress(@Param("user_name") String user_name, @Param("user_tel") String user_tel, @Param("user_sheng") String user_sheng, @Param("user_shi") String user_shi, @Param("user_qu") String user_qu, @Param("user_add") String user_add, @Param("open_id") String open_id);

    //获取地址列表
    @Select("select * from address where open_id=#{open_id} and del_status=0")
    List<Map<String, Object>> getAddressListByOpenId(@Param("open_id") String open_id);

    //获取地址
    @Select("select * from address where address_id=#{address_id} and del_status=0")
    Map<String, Object> getAddressByAddressId(@Param("address_id") int address_id);

    @Delete("update address set del_status=-1 where address_id=#{address_id} ")
    int del_address(@Param("address_id") int address_id);

    @Update("update  address set user_name=#{user_name},user_tel=#{user_tel},user_sheng=#{user_sheng},user_shi=#{user_shi},user_qu=#{user_qu},user_add=#{user_add} where address_id=#{address_id}")
    int updateAddress(@Param("user_name") String user_name, @Param("user_tel") String user_tel, @Param("user_sheng") String user_sheng, @Param("user_shi") String user_shi, @Param("user_qu") String user_qu, @Param("user_add") String user_add, @Param("address_id") int address_id);

    @Select("select * from index_side_img ORDER BY index_side_paper")
    List<Map<String, Object>> getIndex_side_imgS();

    //获取订单列表
    @Select("select *,case order_status when 0 then '待付款' when 1 then '已付款' when 2 then '已安排' when 3 then '已完成' when 4 then '已退款'  end as status,date_format(order_date,'20%y-%c-%d')as d,date_format(order_date,'%H:%i:%s')as t,date_format(show_time,'20%y-%c-%d')as sd,date_format(show_time,'%H:%i')as st from wechat_order left join address on wechat_order.address_id=address.address_id  where  wechat_order.open_id=#{open_id} limit #{page},10")
    List<Map<String, Object>> getOrderList(@Param("open_id") String open_id, @Param("page") int page);

    //获取订单信息
    @Select("select *,case order_status when 0 then '待付款' when 1 then '已付款' when 2 then '已安排' when 3 then '已完成'   end as status,date_format(order_date,'20%y-%c-%d')as d,date_format(order_date,'%H:%i:%s')as t,date_format(response_time,'20%y-%c-%d')as rd,date_format(response_time,'%H:%i:%s')as rt,date_format(show_time,'20%y-%c-%d')as sd,date_format(show_time,'%H:%i')as st,date_format(order_pay_date,'20%y-%c-%d')as pd,date_format(order_pay_date,'%H:%i:%s')as pt,date_format(order_done_time,'20%y-%c-%d')as wd,date_format(order_done_time,'%H:%i:%s')as wt,date_format(order_tuikuan_time,'20%y-%c-%d')as tkd,date_format(order_tuikuan_time,'%H:%i:%s')as tkt from wechat_order left join address on wechat_order.address_id=address.address_id left join manager on manager.manager_id=wechat_order.manager_id left join order_feeScale on wechat_order.order_id=order_feeScale.order_id left join  feeScale on order_feeScale.feeScale_id=feeScale.feeScale_id left join publish on publish.pd_id=feeScale.publish_id  where  wechat_order.order_id=#{order_id}")
    List<Map<String, Object>> getOrderInfo(@Param("order_id") int order_id);

    //获取订单的演员
    @Select("select * from order_feeScale left join order_feeScale_actor on order_feeScale.order_feeScale_id=order_feeScale_actor.order_feeScale_id left join actor on actor.actor_id=order_feeScale_actor.actor_id where order_id=#{order_id}")
    List<Map<String, Object>> getActorListByorder_id(@Param("order_id") int order_id);

    //用户下单
    @Insert("insert into wechat_order values(null,now(),#{order_total_price},#{open_id},null,null,0,#{address_id},null,#{show_time},0,#{order_number},null,null,null,null,null,null)")
    int xiadan(@Param("order_total_price") String order_total_price, @Param("open_id") String open_id, @Param("address_id") int address_id, @Param("show_time") Date show_time, @Param("order_number") String order_number);

    //通过订单编号获取订单
    @Select("select * from wechat_order where order_number=#{order_number}")
    Map<String, Object> getOrderByOrder_number(@Param("order_number") String order_number);

    //添加订单表演内容
    @Insert("insert into order_feescale values(null,#{order_id},#{feeScale_id})")
    int addFeeScaleByorder_id(@Param("order_id") int order_id, @Param("feeScale_id") int feeScale_id);

    //按照openid获取未支付的订单
    @Select("select * from wechat_order where order_status=0 limit 0,1")
    Map<String, Object> getOrderByOpen_id(@Param("open_id") String open_id);

    //支付
    @Update("update wechat_order set order_status=1,order_pay_date=now() where order_number=#{order_number}")
    int pay(@Param("order_number") String order_number);

    //查询新订单短信提示的管理员
    @Select("select * from newOderMsgTelList left join manager on newOderMsgTelList.manager_id=manager.manager_id where del_status=0")
    List<Map<String, Object>> getNewOrderManagerList();

    //删除订单
    @Delete("delete from wechat_order where order_id=#{order_id}")
    int deleteOrder(@Param("order_id") int order_id);

    //订单添加流水号
    @Update("update wechat_order set order_transaction_id=#{transaction_id} where order_number=#{order_number}")
    int updateTransaction(@Param("transaction_id") String transaction_id, @Param("order_number") String order_number);

    //退款查询
    //  select (HOUR(timediff(now(),show_time))*3600+MINUTE(timediff(now(),show_time))*60+SECOND(timediff(now(),show_time)))/3600 as tuikuanjine from wechat_order where order_number='2018112714124152';
    //
//    /select if(72>(HOUR(timediff(now(),show_time))*3600+MINUTE(timediff(now(),show_time))*60+SECOND(timediff(now(),show_time)))/3600>48,(order_total_price*0.7),'') as tuikuanjine,if(48>(HOUR(timediff(now(),show_time))*3600+MINUTE(timediff(now(),show_time))*60+SECOND(timediff(now(),show_time)))/3600>24,(order_total_price*0.5),'') as tuikuanjine ,if((HOUR(timediff(now(),show_time))*3600+MINUTE(timediff(now(),show_time))*60+SECOND(timediff(now(),show_time)))/3600<24,'0元','') as tuikuanjine from wechat_order where order_number='2018112714124152';
    @Select("select *,(HOUR(timediff(now(),show_time))*3600+MINUTE(timediff(now(),show_time))*60+SECOND(timediff(now(),show_time)))/3600 as tuikuanTime   from wechat_order where order_number=#{order_number}")
    Map<String, Object> getRefondTime(@Param("order_number") String order_number);

    //退款成功
    @Update("update wechat_order set order_status=4,out_refund_no=#{out_refund_no},order_tuikuan_time=now()  where order_number=#{order_number}")
    int updateOrderToTuikuan(@Param("out_refund_no") String out_refund_no, @Param("order_number") String order_number);

    //通过订单ID获取客户电话
    @Select("select user_tel from wechat_order left join address on address.address_id=wechat_order.address_id where order_id=#{order_id}")
    String getUser_telByOrder_id(@Param("order_id") int order_id);

    //通过订单ID获取客户电话
    @Select("select user_tel from wechat_order left join address on address.address_id=wechat_order.address_id where order_number=#{order_number}")
    String getUser_telByOrder_number(@Param("order_number") String order_number);

    //修改订单评价
    @Update("update wechat_order set order_grade=#{order_grade}, order_sales=#{order_sales} where order_id=#{order_id}")
    int UpdateOrderSales(@Param("order_grade") int order_grade, @Param("order_sales") String order_sales, @Param("order_id") int order_id);
}
